package com.itheima.controller;


import com.itheima.entity.Emp;
import com.itheima.entity.PageBean;
import com.itheima.entity.Result;
import com.itheima.entity.Yuang;
import com.itheima.service.EmpService;
import com.itheima.util.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/admin/employee")
public class EmaageController {
    @Autowired
    EmpService empService;

    /*
     * 员工管理分页查询员工
     *
     * */
    @GetMapping("/page")
    public Result page(Integer page, Integer PageSize) {
        PageBean pageBean = empService.pageQuery(page, PageSize);
        //3.封装并返回查询结果
        return Result.success(pageBean);
    }
    /*
     * 员工管理添加员工
     *
     * */
    @PostMapping()
    public Result add(@RequestBody Yuang yuang) {
        // 1.接受请求参数
        log.info("添加员工：{}",yuang );
        //2.调用业务层
        empService.add(yuang);
        //3.封装
        return Result.success();
    }
    /*
     * 员工管理根据id查询
     * */
    @GetMapping("/admin/employee")
    public Result queryOne(@PathVariable Integer id) {
        Emp emp = empService.query(id);

        return Result.success(emp);
    }
    /*
     * 员工管理更新员工
     * */

    @PutMapping("")
    public Result update(@RequestBody Yuang yuang) {
        empService.updated(yuang);

        return Result.success();
    }
    /*
    *员工登录，退出
    * */
    @PostMapping("login")
    public Result login(@RequestBody Yuang yuang){
        // 1.调用业务层，实现员工登录功能
        Emp empResult = empService.checkLoging(yuang);
        // 2.封装返返回处理结果
        // 2.1 登录失败
        if (empResult == null) {
            return Result.error("登陆失败,please open your big eyes,check your info");
        }
        //2.2登录成功,颁发JWT令牌(用户身份标识：字符串)
        Map claims = new HashMap();
        claims.put("empId", yuang.getId());
        claims.put("username", yuang.getUsername());
        // 调用工具类，生成令牌
        String jwtTolenStr = JwtUtils.generateJwt(claims);
        //3.封装返回处理结果
        return Result.success(jwtTolenStr);
    }
    /*
     *禁用/启用员工
     * */
    @PostMapping("/status/{status}")
    public Result st(@PathVariable Integer id){
        Emp empResult = empService.st(id);

        return Result.success(empResult);

    }

}
